﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using Cosmetic;

public partial class Controls_CategoryCascadingDropDown : System.Web.UI.UserControl
{
    private CategoryManager m_CategoryManager = new CategoryManager();
    public delegate void SelectedCategoryChangedHandler(Category aCategory);
    public event SelectedCategoryChangedHandler SelectedLv1CategoryChanged;
    public event SelectedCategoryChangedHandler SelectedLv2CategoryChanged;
    public event SelectedCategoryChangedHandler SelectedLv3CategoryChanged;

    private Category t_Category;

    public CategoryManager.ETopCategory TopCategoryName { get; set; }

    public EBool ShowLv2Category { get; set; }
    public EBool ShowLv3Category { get; set; }

    private Category m_TopCategory;
    public Category TopCategory
    {
        get { return m_TopCategory; }
    }

    public Category SaveCategory
    {
        get { return t_Category; }
        set { t_Category = value; }
    }

    public Category SelectedCategory
    {
        get
        {
            if (this.ddlLv3Category.Visible && this.ddlLv3Category.Enabled && this.ddlLv3Category.SelectedValue != "*" && !string.IsNullOrEmpty(this.ddlLv3Category.SelectedValue))
            {
                return this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv3Category.SelectedValue));
            }
            if (this.ddlLv2Category.Visible && this.ddlLv2Category.Enabled && this.ddlLv2Category.SelectedValue != "*" && !String.IsNullOrEmpty(this.ddlLv2Category.SelectedValue))
            {
                return this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv2Category.SelectedValue));
            }
            else if (this.ddlLv1Category.Visible && this.ddlLv1Category.SelectedValue != "*" && !String.IsNullOrEmpty(this.ddlLv1Category.SelectedValue))
            {
                return this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv1Category.SelectedValue));
            }

            {
                return null;
            }
        }
        set
        {
            this.Refresh();

            // if lv1 category contains the selected category
            if (null == value || Guid.Empty == value.CategoryId)
            {
                return;
            }

            if (this.ddlLv1Category.Items.Contains(new ListItem(value.Name, value.CategoryId.ToString())))
            {
                this.ddlLv1Category.SelectedIndex = this.ddlLv1Category.Items.IndexOf(this.ddlLv1Category.Items.FindByValue(value.CategoryId.ToString()));
            }
            else
            {
                // if lv1 category contians the selected category's parent's parent
                if (this.ddlLv1Category.Items.Contains(new ListItem(value.SupCategory.SupCategory.Name, value.SupCategory.SupCategoryId.ToString())))
                {
                    this.ddlLv1Category.SelectedIndex = this.ddlLv1Category.Items.IndexOf(this.ddlLv1Category.Items.FindByValue(value.SupCategory.SupCategoryId.ToString()));
                    this.ddlLv1Category_SelectedIndexChanged(null, null);
                    this.ddlLv2Category.SelectedIndex = this.ddlLv2Category.Items.IndexOf(this.ddlLv2Category.Items.FindByValue(value.SupCategoryId.ToString()));
                    this.ddlLv2Category_SelectedIndexChanged(null, null);
                    this.ddlLv3Category.SelectedIndex = this.ddlLv3Category.Items.IndexOf(this.ddlLv3Category.Items.FindByValue(value.CategoryId.ToString()));
                    this.ddlLv3Category_SelectedIndexChanged(null, null);
                }
                else if (this.ddlLv1Category.Items.Contains(new ListItem(value.SupCategory.Name, value.SupCategoryId.ToString())))
                {
                    this.ddlLv1Category.SelectedIndex = this.ddlLv1Category.Items.IndexOf(this.ddlLv1Category.Items.FindByValue(value.SupCategoryId.ToString()));
                    this.ddlLv1Category_SelectedIndexChanged(null, null);
                    this.ddlLv2Category.SelectedIndex = this.ddlLv2Category.Items.IndexOf(this.ddlLv2Category.Items.FindByValue(value.CategoryId.ToString()));
                    this.ddlLv2Category_SelectedIndexChanged(null, null);
                }
                else
                {
                    this.AddNoDataItemToDropDownList(this.ddlLv1Category);
                    this.AddNoDataItemToDropDownList(this.ddlLv2Category);
                }
            }
        }
    }
    public Category SelectedCategorys
    {
        get
        {
            return this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv1Category.SelectedValue));
            {
                return null;
            }
        }
        set
        {
            this.Refresh();

            // if lv1 category contains the selected category
            if (null == value || Guid.Empty == value.CategoryId)
            {
                return;
            }

            if (this.ddlLv1Category.Items.Contains(new ListItem(value.Name, value.CategoryId.ToString())))
            {
                this.ddlLv1Category.SelectedIndex = this.ddlLv1Category.Items.IndexOf(this.ddlLv1Category.Items.FindByValue(value.CategoryId.ToString()));
            }
            else
            {
                // if lv1 category contians the selected category's parent
                if (this.ddlLv1Category.Items.Contains(new ListItem(value.SupCategory.Name, value.SupCategoryId.ToString())))
                {
                    this.ddlLv1Category.SelectedIndex = this.ddlLv1Category.Items.IndexOf(this.ddlLv1Category.Items.FindByValue(value.SupCategoryId.ToString()));
                    this.ddlLv1Category_SelectedIndexChanged(null, null);
                    this.ddlLv2Category.SelectedIndex = this.ddlLv2Category.Items.IndexOf(this.ddlLv2Category.Items.FindByValue(value.CategoryId.ToString()));
                    this.ddlLv2Category_SelectedIndexChanged(null, null);
                }
                else
                {
                    this.AddNoDataItemToDropDownList(this.ddlLv1Category);
                    this.AddNoDataItemToDropDownList(this.ddlLv2Category);
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //this.m_CategoryManager.CheckTopCategory();
        if (!IsPostBack)
        {
            this.Refresh();
        }
    }

    public void Refresh()
    {
        if (this.ShowLv3Category == EBool.True)
        {
            this.ddlLv2Category.AutoPostBack = true;
            this.ddlLv2Category.Enabled = true;
            this.ddlLv1Category.Enabled = true;
        }
        if (this.ShowLv2Category == EBool.True)
        {
            this.ddlLv1Category.AutoPostBack = true;
            this.ddlLv1Category.Enabled = true;
        }
        else
        {
            this.ddlLv1Category.AutoPostBack = false;
            this.ddlLv1Category.Enabled = true;
            this.ddlLv2Category.Visible = false;
            this.ddlLv3Category.Visible            = false;
        }

        if (null != this.SelectedLv1CategoryChanged)
        {
            this.ddlLv1Category.AutoPostBack = true;
        }
        if (null != this.SelectedLv2CategoryChanged)
        {
            this.ddlLv2Category.AutoPostBack = true;
        }
        if (null != this.SelectedLv3CategoryChanged)
        {
            this.ddlLv3Category.AutoPostBack = true;
        }


        this.m_TopCategory = m_CategoryManager.GetTopCategryByName(TopCategoryName);
        if (null != m_TopCategory)
        {
            this.BindDropDownList(this.ddlLv1Category, m_CategoryManager.GetSubCateogoriesBySupCategory(TopCategory));
        }
        else
        {
            this.m_CategoryManager.CheckTopCategory();
        }
    }

    private void BindDropDownList(DropDownList aDropDownList, List<Category> aCategoryList)
    {
        aDropDownList.Items.Clear();

        if (aCategoryList.Count > 0)
        {
            aDropDownList.DataSource = aCategoryList;
            aDropDownList.DataTextField = "Name";
            aDropDownList.DataValueField = "CategoryId";
            aDropDownList.DataBind();
            aDropDownList.Enabled = true;
            aDropDownList.SelectedIndex = 0;
        }
        else
        {
            this.AddNoDataItemToDropDownList(aDropDownList);
        }

        if ("ddlLv1Category" == aDropDownList.ID)
        {
            this.ddlLv1Category_SelectedIndexChanged(null, null);
        }
        if ("ddlLv2Category" == aDropDownList.ID)
        {
            this.ddlLv2Category_SelectedIndexChanged(null, null);
        }
        if ("ddlLv3Category" == aDropDownList.ID)
        {
            this.ddlLv3Category_SelectedIndexChanged(null, null);
        }
    }

    private void AddNoDataItemToDropDownList(DropDownList aDropDownList)
    {
        aDropDownList.Items.Add(new ListItem("No Data", string.Empty));
        aDropDownList.SelectedIndex = 0;
        aDropDownList.Enabled = false;
    }

    protected void ddlLv1Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        Category theSelectedCategory;

        if (this.ddlLv1Category.SelectedValue == "*" || String.IsNullOrEmpty(this.ddlLv1Category.SelectedValue))
        {
            theSelectedCategory = null;
        }
        else
        {
            theSelectedCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv1Category.SelectedValue));
            if (ShowLv2Category == EBool.True)
            {
                if (String.IsNullOrEmpty(this.ddlLv1Category.SelectedValue))
                {
                    this.ddlLv2Category.Items.Add(new ListItem("No Data", string.Empty));
                    this.ddlLv2Category.Enabled = false;
                    return;
                }

                this.BindDropDownList(this.ddlLv2Category, this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSelectedCategory));
            }
        }

        if (null != this.SelectedLv1CategoryChanged)
        {
            this.SelectedLv1CategoryChanged(theSelectedCategory);
        }
    }

    protected void ddlLv2Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        Category theSelectedCategory;

        if (this.ddlLv2Category.SelectedValue == "*" || String.IsNullOrEmpty(this.ddlLv2Category.SelectedValue))
        {
            theSelectedCategory = null;
        }
        else
        {
            theSelectedCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv2Category.SelectedValue));
            if (ShowLv3Category == EBool.True)
            {
                if (String.IsNullOrEmpty(this.ddlLv2Category.SelectedValue))
                {
                    this.ddlLv3Category.Items.Add(new ListItem("No Data", string.Empty));
                    this.ddlLv3Category.Enabled = false;
                    return;
                }

                this.BindDropDownList(this.ddlLv3Category, this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSelectedCategory));
            }
        }

        if (null != this.SelectedLv2CategoryChanged)
        {
            this.SelectedLv2CategoryChanged(theSelectedCategory);
        }
    }

    protected void ddlLv3Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        Category theSelectedCategory;

        if (this.ddlLv3Category.SelectedValue == "*" || String.IsNullOrEmpty(this.ddlLv3Category.SelectedValue))
        {
            theSelectedCategory = null;
        }
        else
        {
            theSelectedCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.ddlLv3Category.SelectedValue));
        }

        if (null != this.SelectedLv3CategoryChanged)
        {
            this.SelectedLv3CategoryChanged(theSelectedCategory);
        }
    }
}